﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Web;
using blackhouse.JavaScript.JQuery;
using System.Globalization;
using blackhouse.Web.FasterFiles;
using blackhouse.JavaScript;
using mojoPortal.Business.WebHelpers;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Cities;

namespace blackhouse.Aparts.Web.Modules {
	public partial class RollReservation : System.Web.UI.UserControl {

		#region Fields

		private DateTime arrivalDate;
		private DateTime departureDate;
		private int numberOfPeople;
		private bool disabled = false;

		#endregion

		#region Properties



		#endregion

		#region Event Handlers

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			if (Request["submitReservation"] == "1") {
				this.ReadParameters();
				this.RedirectIfValid();
			}
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			if (HttpContext.Current.Items["DisableRollReservation"] != null)
				this.disabled = (bool)HttpContext.Current.Items["DisableRollReservation"];
			if (this.disabled) {
				this.Visible = false;
				return;
			}

			this.AddJavaScript();
		}

		#endregion

		#region Methods

		private void AddJavaScript() {
			JQueryLibraries jql = new JQueryLibraries();
			jql.IncludeJQueryUICulture(CultureInfo.CurrentCulture);
			jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.rollReservation.js,blackhouse.Aparts.Web");
			FasterFilesProvider ffp = new FasterFilesProvider();
			ffp.AddAfterScript("dateFormat = '" + CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern.Replace("MM", "M").Replace("yy", "y") + "';");
			ffp.AddAfterScript("rollReservationEasing = '" + EasingType.EaseOutQuint.ConvertJsReadable() + "';");

			MultilanguageJsResource ml = new MultilanguageJsResource("/App_GlobalResources/ApartsJs.resx");
		}

		private void ReadParameters() {
			DateTime.TryParseExact(Request["reservationStart"], "dd.MM.yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out this.arrivalDate);
			DateTime.TryParseExact(Request["reservationEnd"], "dd.MM.yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out this.departureDate);
			Int32.TryParse(Request["rr-peoples"], out this.numberOfPeople);
		}

		private void RedirectIfValid() {
			if (this.arrivalDate > DateTime.MinValue
				&& this.arrivalDate < this.departureDate
				&& this.numberOfPeople > 0
				&& this.numberOfPeople <= 10) {
				string urlName = GetGlobalResourceObject("Aparts", "apartmentsUrl").ToString();
				SiteCity sc = Factory.Instance().GetSiteCity();
				if (sc != null)
					urlName = urlName + "-" + sc.DefaultName;
				Response.Redirect(UrlUtils.GetReservationUrl(Factory.Instance().GetPreFolderEvaluation(), arrivalDate, departureDate, this.numberOfPeople, 0, urlName), true);
			}
		}

		#endregion

	}
}